
package com.cwises.taglib;

import java.io.IOException;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.TagSupport;


import com.orm.Page;
import com.util.Util;

/**
 * 
* @ClassName: PageTag
* @Description: 分页自定义标签
* @author gaowangxinmin
* @date 2014年5月20日 上午10:02:17
*
 */
public class PageTag extends TagSupport
{
	
	private String property;
	private String url;
	private String className;
	private boolean Multilanguage;



	public int doStartTag()
		throws JspException
	{
		Page<Object> page = new Page<Object>();
		StringBuffer buf = new StringBuffer();
		String total = "";
		String currentPage = "";
		String totalPage = "";
		String ContextPath = "";
		if (getProperty() != null)
		{
			page = (Page) pageContext.getAttribute(getProperty());
			if (page == null)
				page = (Page) pageContext.getRequest().getAttribute(getProperty());
		} else
		{
			page =(Page) pageContext.getAttribute("_DEAFULTDATA");
			if (page == null)
				page =(Page) pageContext.getRequest().getAttribute("_DEAFULTDATA");
		}
		if(Util.isNotNull(page)){
			total = String.valueOf(page.getTotal());
			currentPage = String.valueOf(page.getCurrentPage());
			totalPage = String.valueOf(page.getTotalPage());
		}
		ContextPath = pageContext.getServletContext().getContextPath();
		//不支持多语言
		if(!Multilanguage){
			buf.append("<scri"+"pt id='jqpaginationjs' type='text/javascript' src='"+ContextPath+"/js/jquery/jquery.jqpagination.js'></scr"+"ipt>");
			buf.append("<script type=\"text/javascript\">\n");
			buf.append("$(document).ready(function() {\n");
			buf.append("$('.pagination').jqPagination({\n");
			buf.append("current_page: "+currentPage+",\n");
			buf.append("page_string	: '第{current_page}页，共{max_page}页',\n");
			buf.append("max_page	: "+totalPage+",\n");
			buf.append("paged		: function(pageNum) {\n");
			buf.append("$('.log').prepend('<li>Requested page ' + pageNum + '</li>');\n");
			buf.append("document.srcForm.action=\""+ContextPath+getUrl()+"?currentPage=\"+pageNum;\n");
			buf.append("document.srcForm.submit();\n");
			buf.append("}\n");
			buf.append("});\n ");
			buf.append("}); \n");
			buf.append("</script>\n");
			
			buf.append("<div id=\"pageTlg\" class=\"pagination\">");
			buf.append("<div class=\"pagination_r\">");
			buf.append('共');
			buf.append("<span>").append(total).append("</span>条");  
			buf.append("记录");  
			buf.append("</div>");
			buf.append("<div class=\"pagination_l\">");
			buf.append("<a href=\"#\" class=\"first\" data-action=\"first\" title=\"首页\"></a>"); 
			buf.append("<a href=\"#\" class=\"previous\" data-action=\"previous\" title=\"上一页\"></a>"); 
			buf.append("<input type=\"text\" class=\"textbox\" readonly=\"readonly\" />"); 
			buf.append("<a href=\"#\" class=\"next\" data-action=\"next\" title=\"下一页\"></a>"); 
			buf.append("<a href=\"#\" class=\"last\" data-action=\"last\" title=\"末页\"></a>");
		}
		try
		{
			pageContext.getOut().print(buf.toString());
		}
		catch (IOException e)
		{
			throw new JspTagException((new StringBuilder("Select Tag ERROR:")).append(e.getMessage()).toString());
		}
		return 1;
	}

	public int doEndTag()
		throws JspException
	{
		StringBuffer buf = new StringBuffer();

		buf.append("</div>");
		buf.append("</div>");
		try
		{
			pageContext.getOut().print(buf.toString());
		}
		catch (IOException e)
		{
			throw new JspTagException((new StringBuilder("Select Tag ERROR:")).append(e.getMessage()).toString());
		}
		return 6;
	}


	/**
	 * @return the className
	 */
	public String getClassName() {
		if(Util.isNull(className)){
			className ="default";
		}
		return className;
	}

	/**
	 * @param className the className to set
	 */
	public void setClassName(String className) {
		this.className = className;
	}

	/**
	 * @return the multilanguage
	 */
	public boolean isMultilanguage() {
		return Multilanguage;
	}

	/**
	 * @param multilanguage the multilanguage to set
	 */
	public void setMultilanguage(boolean multilanguage) {
		Multilanguage = multilanguage;
	}

	/**
	 * @return the property
	 */
	public String getProperty() {
		return property;
	}

	/**
	 * @param property the property to set
	 */
	public void setProperty(String property) {
		this.property = property;
	}

	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * @param url the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}

}
